﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using IBatisNet.Extend;
using System.IO;
using System.Configuration;

namespace ExpertMIS.Admin.UserControl
{
    public partial class ColumnForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #endregion

        #region 公有方法

        public ADMColumnEntity Entity
        {
            get
            {
                return ViewState["_ADMColumnEntity_"] as ADMColumnEntity;
            }
            set
            {
                ViewState["_ADMColumnEntity_"] = value;
            }
        }

        public void LoadColumn(string columnOID)
        {
            ADMColumnBroker broker = new ADMColumnBroker();

            Hashtable htQuery = new Hashtable();
            htQuery["OID"] = columnOID;
            Entity = broker.GetEntity(htQuery) as ADMColumnEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as ADMColumnEntity;
            }

            txName.Text = Entity.Name;
            RefreshColumnParent();
            ddlParent.SelectedValue = Entity.ParentID;
            RefreshColumDomainn();
            ddlDomain.SelectedValue = Entity.DomainID;
            txLevel.Text = Entity.Level.ToString();
            txSummary.Text = Entity.Summary;
            txLocation.Text = Entity.Location;
        }

        public bool SaveColumn()
        {
            try
            {
                ADMColumnBroker broker = new ADMColumnBroker();

                Entity.Name = txName.Text;
                Entity.ParentID = ddlParent.SelectedValue;
                Entity.DomainID = ddlDomain.SelectedValue;
                int iLevel = 0;
                int.TryParse(txLevel.Text, out iLevel);
                Entity.Level = (short)iLevel;
                Entity.Summary = txSummary.Text;
                Entity.Location = txLocation.Text;

                ///////////////////////////////////////////////////////////
                // Column Photo
                if (fuImage.HasFile)
                {
                    string filename = Guid.NewGuid().ToString() + "." + System.IO.Path.GetExtension(fuImage.FileName);
                    string filepath = Server.MapPath(ConfigurationManager.AppSettings["UserUpload"]);
                    fuImage.SaveAs(filepath + filename);
                    Entity.ImagePath = filename;
                }

                broker.SaveEntity(Entity, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void RefreshColumnParent()
        {
            ADMColumnBroker broker = new ADMColumnBroker();
            IList<ADMColumnEntity> list = broker.GetAllColumns();

            ADMColumnEntity entity = new ADMColumnEntity();
            entity.OID = string.Empty;
            entity.Name = "顶级栏目";
            list.Insert(0, entity);

            ddlParent.DataTextField = "Name";
            ddlParent.DataValueField = "OID";

            ddlParent.DataSource = list;
            ddlParent.DataBind();
        }

        public void RefreshColumDomainn()
        {
            DomainBroker broker = new DomainBroker();
            ddlDomain.DataSource = broker.GetAllDomains();
            ddlDomain.DataTextField = "DomainName";
            ddlDomain.DataValueField = "OID";
            ddlDomain.DataBind();
        }

        #endregion
    }
}